﻿using NotificationsExtensions.TileContent;
using ReferenceTemplate.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI;
using Windows.UI.Notifications;
using Windows.UI.StartScreen;

namespace ReferenceTemplate.Windows8.Services
{
    class TileService : ITileService
    {
        public async void PintToStart(Models.Link link)
        {
            Uri uri = new Uri("ms-appx:///Assets/Logo.scale-100.png");
            SecondaryTile secondaryTile = new SecondaryTile(
                    link.Id.ToString(), 
                    link.Title, 
                    link.Id.ToString(), 
                    uri, // Logo
                    TileSize.Square150x150);

            //Ponemos el color de la categoría como color del Tile
            
            //Indicamos que queremos que se vea el nombre en todos los tamaños
            secondaryTile.VisualElements.ShowNameOnWide310x150Logo = true;
            secondaryTile.VisualElements.ShowNameOnSquare310x310Logo = true;
            secondaryTile.VisualElements.ShowNameOnSquare150x150Logo = true;

            //Indicamos el nombre que se mostrará
            secondaryTile.DisplayName = link.Title;

            //Asignamos la imagen que se va a mostrar en el Tile
            secondaryTile.VisualElements.Square30x30Logo = uri;
            secondaryTile.VisualElements.Square150x150Logo = uri;
            secondaryTile.VisualElements.Square310x310Logo = uri;
            secondaryTile.VisualElements.Square70x70Logo = uri;

            secondaryTile.VisualElements.Wide310x150Logo = new
            Uri("ms-appx:///Assets/Wide310x150Logo.scale-100.png");

            bool pin = await secondaryTile.RequestCreateAsync();

            if (pin)
            {
                #region Live Tile
                // Creo el Live Tile
                var updater = TileUpdateManager.CreateTileUpdaterForSecondaryTile(secondaryTile.TileId);

                updater.EnableNotificationQueue(true);
                updater.Clear();

                var item = link;

                // Tile grande
                ITileSquare310x310ImageAndText01 tile310x310 = TileContentFactory.CreateTileSquare310x310ImageAndText01();
                tile310x310.TextCaptionWrap.Text = item.Title;
                tile310x310.Image.Src = "ms-appx:///Assets/Images/" + item.Image;

                // Tile ancho
                ITileWide310x150ImageAndText01 tile310x150 = TileContentFactory.CreateTileWide310x150ImageAndText01();
                tile310x150.TextCaptionWrap.Text = item.Title;
                tile310x150.Image.Src = "ms-appx:///Assets/Images/" + item.Image;

                // Tile mediano
                ITileSquare150x150PeekImageAndText04 tile150x150 = TileContentFactory.CreateTileSquare150x150PeekImageAndText04();
                tile150x150.TextBodyWrap.Text = item.Title;
                tile150x150.Image.Src = "ms-appx:///Assets/Images/" + item.Image;

                tile310x310.Wide310x150Content = tile310x150;
                tile310x150.Square150x150Content = tile150x150;

                updater.Update(new TileNotification(tile310x310.GetXml()));

                #endregion 
            }
        }

        public void UnpinToStart(Models.Link link)
        {
            throw new NotImplementedException();
        }

        public bool IsPinnedToStart(Models.Link link)
        {
            return SecondaryTile.Exists(link.Id);
        }
    }
}
